<template>
  <div class="app-container">
    <div>
      <el-button type="primary" class="add-button" size="normal" @click="openAddDialog">添加</el-button>
      <br><br>
    </div>
    <el-table
      v-loading="listLoading"
      :data="page.list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="岗位编码" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.postCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位名字" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.postName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.postSort }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row.postId)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.postId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <br><br>
    <!-- 分页 -->
    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[10,20,50,100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加弹窗 -->
    <el-dialog title="添加" :visible.sync="addDialog">
      <el-form ref="form" :model="post" label-width="200px" size="normal">
        <el-form-item label="岗位编码">
          <el-input v-model="post.postCode"/>
        </el-form-item>
        <el-form-item label="岗位名字">
          <el-input v-model="post.postName"/>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="post.postSort"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="normal" @click="onSubmit">提交</el-button>
          <el-button size="normal" @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 修改弹窗-->
    <el-dialog title="修改" :visible.sync="updateDialog">
      <el-form ref="form" :model="post" label-width="200px" size="normal">
        <el-form-item label="岗位编码">
          <el-input v-model="post.postCode"/>
        </el-form-item>
        <el-form-item label="岗位名字">
          <el-input v-model="post.postName"/>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="post.postSort"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="normal" @click="onSubmitUpdate">提交</el-button>
          <el-button size="normal" @click="onCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { save, delById, findPage, getById, updateById } from '@/api/post'

export default {
  components: {
    // eslint-disable-next-line vue/no-unused-components
  },
  data() {
    return {
      list: null,
      listLoading: true,
      // 控制添加弹窗
      addDialog: false,
      //  控制修改弹窗
      updateDialog: false,
      post: {},
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [] // 数据
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      findPage(this.page).then(response => {
        this.page = response.page
        // console.log(response.data)
        this.listLoading = false
      })
    },
    handleEdit(id) {
      // 修改
      getById(id).then(res => {
        // eslint-disable-next-line no-empty
        if (res.code !== 20000) {
          this.$message.success(res.error)
        }
        this.post = res.data
        this.updateDialog = true
      })
    },
    closeUpdateDialog() {
      // 关闭修改弹窗
      this.updateDialog = false
    },
    openAddDialog() {
      // 控制添加弹窗打开
      this.addDialog = true
    },
    closeAddDialog() {
      // 控制添加弹窗关闭
      this.addDialog = false
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        delById(id).then(res => {
          this.$message.success(res.message)
          this.fetchData()
        })
      })
    },
    //  分页切换
    handleSizeChange(val) {
      this.page.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.fetchData()
    },
    // 新增调用方法
    onSubmit() {
      save(this.post).then(res => {
        // console.log(res)
        if (res.code !== 20000) {
          this.$message.error(res.message)
          this.fetchData()
          this.addDialog = false
          this.post = {}
        } else {
          this.$message.success(res.message)
          this.fetchData()
          this.addDialog = false
          this.post = {}
        }
      })
    },
    onCancel() {
      this.addDialog = false
      this.updateDialog = false
    },
    onSubmitUpdate() {
      updateById(this.post).then(res => {
        this.$message.success(res.message)
        this.fetchData()
        this.updateDialog = false
      })
    }
  }
}
</script>
